package morenoapp.components.input;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumn;
import morenoapp.MorenoApp;
import morenoapp.components.common.table.renderer.TooltipDefaultTableCellRenderer;
import morenoapp.components.help.MorenoHelp;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author root
 */
public class InputAnswersTableJPanel extends javax.swing.JPanel implements MorenoRespondentSelecetionListener {
    /** Creates new form InputAnswersTableJPanel */
    public InputAnswersTableJPanel() {
        initComponents();
        initComponentsCustom();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        scrollTable = createAnswersScrollPane();
        tableAnswers = createAnswersTable();
        toolbar = new javax.swing.JToolBar();
        jCheckBoxShortView = new javax.swing.JCheckBox();

        setName("Form"); // NOI18N

        scrollTable.setName("scrollTable"); // NOI18N

        tableAnswers.setModel(getTableModel());
        tableAnswers.setName("tableAnswers"); // NOI18N
        scrollTable.setViewportView(tableAnswers);

        toolbar.setFloatable(false);
        toolbar.setRollover(true);
        toolbar.setName("toolbar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(InputAnswersTableJPanel.class);
        jCheckBoxShortView.setText(resourceMap.getString("jCheckBoxShortView.text")); // NOI18N
        jCheckBoxShortView.setFocusable(false);
        jCheckBoxShortView.setName("jCheckBoxShortView"); // NOI18N
        jCheckBoxShortView.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${tableModel.shortView}"), jCheckBoxShortView, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        toolbar.add(jCheckBoxShortView);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
            .addComponent(scrollTable, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollTable, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxShortView;
    private javax.swing.JScrollPane scrollTable;
    private javax.swing.JTable tableAnswers;
    private javax.swing.JToolBar toolbar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    InputAnswersTableModel tableModel = new InputAnswersTableModel();

    public InputAnswersTableModel getTableModel() {
        return tableModel;
    }

    @Override
    public void onRespondentSelected(MorenoRespondentFace aRespondent) {
        getTableModel().setRespondent(aRespondent);
    }

    /**
     * Создаёт таблицу со специальными методами отрисовки различных состояний.
     * @return
     */
    protected JTable createAnswersTable() {
        return new JTable();
    }

    protected JScrollPane createAnswersScrollPane() {
        return new JScrollPane() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                String statusMessage = tableModel.getStatusMessage();
                if (statusMessage != null) {
                    MorenoUtil.drawStringAtComponentCenter(g, this, Color.BLACK, null, statusMessage);
                }
            }
        };
    }
    private void initComponentsCustom() {
        toolbar.add(MorenoHelp.createHelpButton("input_answers_panel"));
        tableAnswers.setRowHeight(25);
        TableColumn columnLabel = tableAnswers.getColumnModel().getColumn(0);
        columnLabel.setMaxWidth(60);
        columnLabel.setMinWidth(columnLabel.getMaxWidth());
        TableColumn columnAnswer = tableAnswers.getColumnModel().getColumn(1);
        columnAnswer.setCellEditor(new InputAnswersTableCellEditor(getTableModel()));
        columnAnswer.setCellRenderer(new TooltipDefaultTableCellRenderer());

        //add repaint listener
        tableAnswers.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                scrollTable.repaint(scrollTable.getBounds());
            }
        });

        MorenoApp.getModel().addModelListener(new MorenoModelAdapter(){
            @Override
            public void onRespondentChanged(MorenoRespondentFace changed) {
                getTableModel().reset();
            }
            @Override
            public void onRespondentsListChanged() {
                getTableModel().reset();
            }
            @Override
            public void onWholeModelChanged() {
                getTableModel().reset();
            }
        });
    }
}
